<style lang="less">
.hmCouponUser {
    .ivu-modal {
        width: 660px !important;
    }
}
</style>
<template>
    <Modal v-model="show" :title="editTitle" class="hmCouponUser" :mask-closable="false">
        <Form ref="hmCouponUserForm" :model="hmCouponUserForm" :rules="hmCouponUserFormRule" :label-width="100">
            <FormItem label="优惠券ID" prop="couponId">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUserForm.couponId" placeholder="请输入优惠券ID"/>
            </FormItem>
            <FormItem label="用户ID" prop="userId">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUserForm.userId" placeholder="请输入用户ID"/>
            </FormItem>
            <FormItem label="领券时间" prop="receiveTime">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUserForm.receiveTime" placeholder="请输入领券时间"/>
            </FormItem>
            <FormItem label="优惠券状态 0:失效 1:有效 2:使用过" prop="status">
                <RadioGroup v-bind:disabled="disabled" v-model="hmCouponUserForm.status">
                    <Radio v-for="item in statusRadio" :label="item.value">{{ item.title }}</Radio>
                </RadioGroup>
            </FormItem>
            <FormItem label="开始时间" prop="userStartTime">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUserForm.userStartTime" placeholder="请输入开始时间"/>
            </FormItem>
            <FormItem label="结束时间" prop="userEndTime">
                <Input v-bind:disabled="disabled" type="text" :maxlength=50 v-model="hmCouponUserForm.userEndTime" placeholder="请输入结束时间"/>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="text" @click="show=false">取消</Button>
            <Button type="primary" :loading="loading" @click="handSubmit" v-if="!disabled">提交</Button>
        </div>
    </Modal>
</template>
<script>
import {addHmCouponUser, updateHmCouponUser, getHmCouponUser} from '@/api/hmzd/hmCouponUser'

export default {
    name: "updateHmCouponUser",
    props: {
        value: {
            type: Boolean,
            default: false
        },
        HmCouponUserId: {
            type: String
        },
        modalTitle: {
            type: String
        }
    },
    data() {
        return {
            show: this.value,
            editTitle: this.modalTitle,
            loading: true,
            disabled: false,
            statusRadio: [],
            hmCouponUserForm: {
                couponId: '',
                userId: '',
                receiveTime: '',
                status: '',
                userStartTime: '',
                userEndTime: '',
            },
            hmCouponUserFormRule: this.getHmCouponUserFormRule()
        }
    },
    methods: {
        handSubmit() {
            this.$refs['hmCouponUserForm'].validate((valid) => {
                this.loading = true;
                if (valid) {
                    if (this.HmCouponUserId != null && this.HmCouponUserId.trim().length > 0) {
                        this.hmCouponUserForm.id = this.HmCouponUserId;
                        updateHmCouponUser(this.hmCouponUserForm).then(res => {
                            if (res.success) {
                                this.closeModal(false);
                                this.$emit('handSearch');
                                this.$Message.success('保存成功');
                            } else {
                                this.$Message.error(res.msg);
                            }
                        }).finally(() => {
                            this.loading = false;
                        });
                    } else {
                        addHmCouponUser(this.hmCouponUserForm).then(res => {
                            if (res.success) {
                                this.closeModal(false);
                                this.$emit('handSearch');
                                this.$Message.success('保存成功');
                            } else {
                                this.$Message.error(res.msg);
                            }
                        }).finally(() => {
                            this.loading = false;
                        });
                    }
                } else {
                    this.loading = false;
                    this.$Message.error('表单验证不通过！');
                }
            });
        },
        closeModal(val) {
            this.$emit('input', val);
        },
        initForm() {
            this.hmCouponUserForm = {
                couponId: '',
                userId: '',
                receiveTime: '',
                status: '',
                userStartTime: '',
                userEndTime: '',
            };
        },
        getHmCouponUserFormRule() {
            return {
                couponId: [
                    {required: true, message: '优惠券ID不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                userId: [
                    {required: true, message: '用户ID不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                receiveTime: [
                    {required: true, message: '领券时间不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                status: [
                    {required: true, message: '优惠券状态 0:失效 1:有效 2:使用过不能为空！', trigger: 'blur', pattern: /.+/},
                ],
                userStartTime: [
                    {required: true, message: '开始时间不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
                userEndTime: [
                    {required: true, message: '结束时间不能为空！', trigger: 'blur'},
                    {type: 'string', max: 50, message: '数据的最大长度为50！', trigger: 'blur'}
                ],
            }
        }
    },
    watch: {
        value(val) {
            this.show = val;
        },
        show(val) {
            this.initForm();
            this.loading = false;
            this.editTitle = this.modalTitle;
            if (this.editTitle == "查看") {
                this.disabled = true;
            } else {
                this.disabled = false;
            }
            if (val) {
                this.$refs['hmCouponUserForm'].resetFields();
                this.$refs['hmCouponUserForm'].id = null;
                if (this.HmCouponUserId != null && this.HmCouponUserId.trim().length > 0) {
                    getHmCouponUser({id: this.HmCouponUserId}).then(res => {
                        if (res.success) {
                            this.hmCouponUserForm = res.data;
                        } else {
                            this.$Message.error(res.msg);
                        }
                    });
                }
            } else {
                this.closeModal(val);
            }
        }
    }
}
</script>
